/**
 * Created by wenchuan on 2018/5/4.
 */
define(function () {

    var self,
        app = win.app,
        others = {
            ischecked: '0'
        },
        subOrders = []
    ;

    var OrderTemp = app.Module.extend({

        init: function () {
            self = this;
            self.mData = {};
            self._super({
                _tag: 'p_ordertemp', // key, 对象放进pageModule
                _refreshAutoInit: true, //自动加载刷新控件
                limit: 100
            }, win);
        },
        subData: function () {
            app.ajax({
                xurl: "api/wx/gwat100/updateData",
                loading: true,
                errmsg: '提交操作异常',
                xdata: {
                    obj: app.jsonString({orders: subOrders})
                },
                cb: function (result) {
                    if (result.ok) {
                        app.openPage({
                            src: "yxsjxd/order-submit.html",
                            argsKey: 'ordersubmit_page',
                            args: {
                                items: self._getRefreshObj().data.filterMaps(subOrders),
                                cb:function () {
                                    self._refresh();
                                }
                            }
                        });
                    }
                }
            });
        },
        deleteTemp:function (val) {
            app.ajax({
                xurl: "api/wx/gwat100/delete",
                loading: true,
                errmsg: '删除操作异常',
                xdata: {
                    GWCMDOCNO: val.GWCMDOCNO,
                    GWCMSEQ:val.GWCMSEQ
                },
                cb: function (result) {
                    if (result.ok) {
                        app.toast('删除成功');
                        self._getRefreshObj().data.remove(val);
                    }
                }
            });
        },
        _initRefresh: function () { // 实例化刷新组件
            if (self._refreshIds.length == 0) {
                self._refreshIds = ['scroll1'];
            }
            this._super(win);
        },
        _refreshCb: function (goods) {// 刷新组件回调函授
            var _super = this._super;
            app.ajax({
                xurl: "api/wx/gwat100/getGwcmDataPage",
                xdata: {
                    page:goods.page+1,
                    limit: goods.limit
                },
                cb: function (result) {
                    if (result.ok) {
                        var data = result.data.data;
                        goods.data.pushArray(data.addMap({
                            ischecked: '0'
                        }), goods.page == 0);

                        _super({
                            obj: goods,
                            dataLen: data.length,
                            nodata: '无暂存单信息...'
                        });
                    }
                }
            });
        },
        _lazy: function () {
            this._super();
            new Vue({
                el: app.getByid(doc, 'contentFooter'),
                data: others,
                methods: {
                    subOrder: function () {
                        subOrders.removeAll();
                        self._getRefreshObj().data.forEach(function (v) {
                            //if (v.ischecked == '1') {
                                subOrders.push({
                                    GWCMDOCNO: v.GWCMDOCNO,
                                    GWCMSEQ: v.GWCMSEQ,
                                    GWCM003: v.GWCM003,
                                    GWCM004: v.GWCM004
                                });
                            //}
                        });
                        if (subOrders.length > 0) {
                            app.confirm({
                                msg: '确定是否提交？',
                                ok: function (e) {
                                    self.subData();
                                }
                            });
                        }
                    },
                    checkChange: function () {
                        self._getRefreshObj().data.addMap({
                            ischecked: others.ischecked
                        }, true);
                    }
                }
            });
        },
        _ready: function () {
            Vue.component('ordertemp-listitem', {
                template: app.getByid(doc, 'cartitem-template'),
                props: {
                    model: Array
                },
                methods: {
                    minus: function (val) {
                        if (val.GWCM003 > 1) {
                            val.GWCM003--;
                        }
                    },
                    plus: function (val) {
                        val.GWCM003++;
                    },
                    showDetail:function (val) {
                        app.openPage({
                            src: "yxsjxd/classes-info.html",
                            argsKey: 'classesinfo_page',
                            args: {
                                type:'order-temp',
                                item: val,
                                cb: function () {

                                }
                            }
                        });
                    },
                    del:function (val, elobj) {
                        app.confirm({
                            msg:'确定删除？',
                            ok:function (e) {
                                self.deleteTemp(val);
                                mui.swipeoutClose(Zepto(elobj.target).parents("li").get(0));
                            }
                        });
                    }
                }
            });
        }
    });

    return new OrderTemp();

});